package engine.newUi
{
	import engine.data.ModelLocator;
	import engine.net.datatype.WString;
	import engine.net.event.CustomMsgEvent;
	import engine.net.messages.CustomToClientFlag;
	import engine.net.messages.CustomToServerFlag;
	import engine.net.mgr.SystemSender;
	import engine.ui.AlertUI;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.utils.ByteArray;
	
	import newx.com.config.StaticIni;
	import newx.com.control.window.NxWindowEvent;

	/**
	 * 发送邮件逻辑 
	 * @author liuel
	 * 
	 */	
	public class SendMailLogic
	{
		private static var _self		:SendMailLogic;
		private		 var _ui		:SendMailUI;
		/**附件的个数*/
		public var itemCount		:int		=	5;		
		/**附件的宽度*/
		public var itemWidth		:int		=	40;		
		
		/**附件的高度*/
		public var itemHeight		:int		=	40;	
		
		private var _isVisible      :Boolean;
		
		private var _model:engine.data.ModelLocator;
		
		private var _itemList:Array = [];
		public static function getInstance():SendMailLogic
		{
			if(_self == null)
			{
				_self = new SendMailLogic();
			}
			return _self;
		}
		public function SendMailLogic()
		{
		}
		 public function Init() : void
		{
			 _ui = SendMailUI.getInstance();
			 _ui.txtAddressee.wordWrap = false;
			 
			 _ui.txtTitle.maxChars = 10;
			 _ui.txtcontent.width =510;
			 _ui.txtcontent.maxChars = 200;
			 _ui.txtcontent.wordWrap = true;
			 _ui.txtcontent.multiline = true;
			 _ui.txtcontent.autoScroll = true;
			 _itemList.length = itemCount;
			 
			 _model = ModelLocator.getInstance();
			 
			 //确认可以发送邮件了
			 _model.addEventListener(String(CustomToClientFlag.
				 SERVER_CUSTOMMSG_LETTER_SEND),sendMail);
		}
		 public function	BeforeShow():void
		{
			 _ui.txtTitle.text = "";
			 _ui.txtcontent.text = "";
			 _ui.txtAddressee.text = "";
			 _ui.currTxt.text ="";
			 _isVisible = true;
			 addListener();
		}
		public function	AfterHide():void
		{
			clear();
			_isVisible = false;
			removeListener();
		}
//		public function addItem(obj:ObjectData):Boolean{
//			//当时绑定的道具的时候，不能添加
//			if(obj.GetPropertiesEx("BindStatus") == 1){
//				AlertUI.showAlert(StaticIni.getSwfLangStr("MessageUI_Notice"),StaticIni.getSwfLangStrVar(("5118"),[" "+StaticIni.getSwfLangStr(obj.GetPropertiesEx("NameID"))+" "]));
//				return false;
//			}
//			if(_itemList.indexOf(obj)!= -1)return false;
//			for(var i:int = 0;i < itemCount; i++){
//				if(_itemList[i] == null){
//					_itemList[i] = (obj);
//					addItemImage(obj,i);
//					break;
//				}
//			}
//			return true;
//		}
//		private function hasItem():Boolean
//		{
//			for(var i:int = 0; i < itemCount;i++){
//				if(_itemList[i] != null){
//					return true;
//				}
//			}
//			return false;
//		}
//		/**
//		 * 添加道具图片
//		 */
//		private function addItemImage(itemObject:ObjectData,itemPos:int):void
//		{
//			var itemMC:MovieClip =	_ui.gridGroup.getItemByIndex(itemPos);
//			var img:NxClip = new NxClip();
//			img.width =	itemWidth;
//			img.height = itemHeight;
//			img.x =	img.y =	2;
//			img.source = ResourcesPath.getPropsURL(itemObject.GetProperties("Photo"))//"res/items/icon_"+$itemObject.GetProperties("Photo")+".jpg";
//			itemMC.addChild(img);
//			
//			//当时有数量的物品时，显示物品
//			if(itemObject.CheckProperties("Amount")){
//				var txtCount:NxLabel =	new NxLabel();
//				txtCount.htmlStr = itemObject.GetPropertiesEx("Amount");
//				itemMC.addChild(txtCount);
//			}
//			itemMC["data"]	= itemObject;
//		}
		private function isSendMail():Boolean{
			if(_ui.txtAddressee.text == ""){
				AlertUI.showAlert(StaticIni.getSwfLangStr("MessageUI_Notice"),StaticIni.getSwfLangStr("MessageUI_EmailNoReceiver"));
				return false;
			}
			if(_ui.txtTitle.text == ""){
				AlertUI.showAlert(StaticIni.getSwfLangStr("MessageUI_Notice"),StaticIni.getSwfLangStr("MessageUI_EmailNoTitle"));
				return false;
			}
			if(_ui.txtcontent.text == ""){
				AlertUI.showAlert(StaticIni.getSwfLangStr("MessageUI_Notice"),StaticIni.getSwfLangStr("MessageUI_EmailNoContent"));
				return false;
			}
			if(getContentLeng( _ui.txtcontent.text) > 500){
				AlertUI.showAlert(StaticIni.getSwfLangStr("MessageUI_Notice"),StaticIni.getSwfLangStr("UIStr_1000828"));
				return false;
			}
			var myName:String = _model.scene.Role.GetPropertiesEx("Name");
			if(myName == _ui.txtAddressee.text){
				AlertUI.showAlert(StaticIni.getSwfLangStr("MessageUI_Notice"),StaticIni.getSwfLangStr("60000"));
				return false;
			}
			return true;
		}
		/**
		 * 	获得内容长度，字节》256出错
		 * 
		 */
		private function getContentLeng (content : String) : int
		{
			var bytes:ByteArray = new ByteArray();
			
			bytes.writeMultiByte(content, "gb2312");
			
			bytes.position = 0;
			
			return bytes.length;
		}
		/**
		 *	询问服务器是否可以发送 
		 * 
		 */
		private function querySend(e : Event) : void
		{
			if(!isSendMail())return;
			
			SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LETTER_CAN_SEND, new WString(_ui.txtAddressee.text));
		}
		
		/**
		 * 发送邮件
		 */
		private function sendMail(e : CustomMsgEvent):void
		{
			var addressee	: 	WString 		=	new WString(e.pList[0]);
			var title		:	WString			=	new WString(_ui.txtTitle.text);
			var content		:	WString			=	new WString(_ui.txtcontent.text);
//			if(!hasItem()){
				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LETTER_SEND,
					addressee, title, content);
//			}else{
//				var  sendItemStr:String = "";
//				for(var i:int = 0;i<itemCount;i++){
//					var item:Object = _itemList[i];
//					if(item == null) continue;
//					var amount:int = 1;
//					if(item.CheckProperties("Amount")){
//						amount = item.GetProperties("Amount");
//					}
//					sendItemStr += View.VIEWPORT_TOOL + ","+item.ObjKey + "," + amount + ";";
//				}
//				SystemSender.getInstance().CustomMsg(CustomToServerFlag.CLIENT_CUSTOMMSG_LETTER_SEND,
//					addressee, title, content);
//			}
			_ui.hide();
		}
//		private function onClickClip(e:MouseEvent):void{
//			clearItem(e.currentTarget as MovieClip);
//		}
		
		private function addListener():void
		{
			_ui.windowBG.addEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			_ui.btnsend.addEventListener(MouseEvent.CLICK,querySend);
			_ui.txtcontent.addEventListener(MouseEvent.ROLL_OVER,onChange)
			_ui.txtcontent.addEventListener(Event.CHANGE,onChange);
//			_ui.gridGroup.addItemsEvent(MouseEvent.ROLL_OVER,onItemOver);
//			_ui.gridGroup.addItemsEvent(MouseEvent.ROLL_OUT,onItemOut);
//			_ui.gridGroup.addItemsEvent(MouseEvent.CLICK,onClickClip);
		}
		
		private function removeListener():void
		{
			_ui.windowBG.removeEventListener(NxWindowEvent.WINDOW_CLOSE,onClose);
			_ui.btnsend.removeEventListener(MouseEvent.CLICK,querySend);
			_ui.txtcontent.removeEventListener(MouseEvent.ROLL_OVER,onChange)
			_ui.txtcontent.removeEventListener(Event.CHANGE,onChange);
//			_ui.gridGroup.removeItemsEvent(MouseEvent.ROLL_OVER,onItemOver);
//			_ui.gridGroup.removeItemsEvent(MouseEvent.ROLL_OUT,onItemOut);
//			_ui.gridGroup.removeItemsEvent(MouseEvent.CLICK,onClickClip);
			
		}
		
		private function onClose(e:Event):void
		{
			_ui.hide();
		}
		
		private function clear():void{
			_ui.txtTitle.text = "";
			_ui.txtcontent.text = "";
			_ui.txtAddressee.text = "";
			_ui.currTxt.text ="";
//			clearItemAll();
		}
		
//		private function clearItemAll():void
//		{
//			for(var i:int = 0;i< itemCount;i++){
//				if(_itemList[i] != null){
//					clearItem(_ui.gridGroup.getItemByIndex(i));
//				}
//			}
//		}
		
		
//		private function clearItem(itemMC:MovieClip):void
//		{
//			if(itemMC["data"] != null){
////				PackUI.getInstance().removeShield($itemMC["data"]);
//			}
//			
//			while(itemMC.numChildren > 1){
//				itemMC.removeChildAt(1);
//			}
//			var index:int = _itemList.indexOf(itemMC["data"]);
//			itemMC["data"] = null;
//			if(index == -1)return;
//			_itemList[index] = null;
//		}
			
		public function getVisible():Boolean{
			return _isVisible;
		}
		
		public function isFull() : Boolean
		{
			if(_itemList[itemCount - 1] == undefined)return false;
			
			for each(var mail : * in _itemList){
				if(mail == undefined || mail == null){
					return false;
				}
			}
			return true;
		}
		
		public function revertMail(addressee : String , title : String):void{
			_ui.showOrHide();
			_ui.txtAddressee.text = addressee;
			var newStr:String="回复:"
			if(title.length+newStr.length>10)
			{
				_ui.txtTitle.text = title;
			}
			else
			{
				_ui.txtTitle.text = newStr+title;
			}
			
		}
		
		public function get itemList():Array
		{
			return _itemList;
		}
		private function onChange(e:Event):void
		{
			var val:int =_ui.txtcontent.maxChars-_ui.txtcontent.text.length;
			_ui.currTxt.htmlText =StaticIni.getSwfLangStrVar("UIString_1000109",[val]);
		}

	}
}